import type {ProxyGraphService} from "@/business/proxy/graph.proxy";
import type {Graph} from "@/business/entity/graph/graph.entity";

/**
 *  绘图注解类
 * @author JiangXincan
 * @date 2023/01/29 13:56
 */
export class DrawAnnotation {

    /**
     *  绘图之前准备阶段，开启绘图，设置图形样式
     *  @param proxyGraphService 当前绘图代理实例化类
     * @param graph 图形实例
     * @author JiangXincan
     * @date 2023/01/29 13:56
     */
    private static beforeDraw(proxyGraphService: ProxyGraphService, graph: Graph): void {
        console.log(`绘图【准备阶段】：将要绘制${graph.id}图形`);
        proxyGraphService.ctx.beginPath();
        proxyGraphService.ctx.lineWidth = graph.style.lineWidth;
        proxyGraphService.ctx.strokeStyle = graph.style.color;
        proxyGraphService.ctx.fillStyle = graph.style.color;

    }

    /**
     *  绘图结束，关闭绘图
     * @param proxyGraphService 当前绘图代理实例化类
     * @param graph 图形实例
     * @author JiangXincan
     * @date 2023/01/29 13:56
     */
    private static afterDraw(proxyGraphService: ProxyGraphService, graph: Graph): void {
        proxyGraphService.ctx.closePath();
        proxyGraphService.ctx.fill();
        proxyGraphService.ctx.stroke();
        console.log(`绘图【结束阶段】：结束绘制${graph.id}图形`);
    }


    /**
     *  绘图装饰器
     * @author JiangXincan
     * @date 2023/01/29 13:56
     */
    public static DrawGraph() {
        return  (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
            // 获取ProxyGraphService 工厂内部 graphDraw 函数，也就是，被装饰器装饰的函数
            const graphDraw = descriptor.value;
            return {
                configurable: true,
                get() {
                    return ((...args: any) => {
                        // @ts-ignore
                        DrawAnnotation.beforeDraw(this, args[0])
                        graphDraw.bind(this)(...args);
                        // @ts-ignore
                        DrawAnnotation.afterDraw(this, args[0]);
                    }).bind(this);
                }
            }
        };
    }
}